'use strict';

const BaseController = require('./base');

class CommentController extends BaseController {
  get _service() {
    return 'comment';
  }

  // 修改状态
  async _editStatus(status) {
    const { ctx } = this;
    const { id } = ctx.params();
    const result = await this._getService().update({ status, id });
    if (result) {
      this.success();
    } else {
      this.error('更新论坛失败');
    }
  }

  async create() {
    const { ctx } = this;
    const { title, content, pictures, houseId } = ctx.params();
    const result = await this._getService().create({ title, content, pictures, houseId, status: 1, uid: ctx.userId });
    if (result) {
      this.success();
    } else {
      this.error('创建论坛失败');
    }
  }

  // 更新论坛基本数据
  async update() {
    const { ctx } = this;
    const { title, content, pictures } = ctx.params();
    const { id } = ctx.params;
    const result = await this._getService().update({ title, content, pictures, status: 1, id });
    if (result) {
      this.success();
    } else {
      this.error('更新论坛失败');
    }
  }

  async agree() {
    await this._editStatus(2);
  }

  async disagree() {
    await this._editStatus(3);
  }

  async destroy() {
    await this._destory('删除论坛失败');
  }

  async show() {
    await this._show('获取论坛失败');
  }

  // 获取所有论坛
  async index() {
    const { ctx } = this;
    const { title, status, prop, sort, pageIndex, pageSize } = ctx.params();
    const { createdAt } = ctx.queries;
    const tempResult = await this._getService().findList({ title, status, prop, sort, pageIndex, pageSize, createdAt });
    const result = ctx.helper.flat(tempResult.data);
    if (result) {
      this.success(result, tempResult.count);
    } else {
      this.error('获取论坛失败');
    }
  }

  // 获取登录人的论坛
  async indexMy() {
    const { ctx } = this;
    const { status, prop, sort, pageIndex, pageSize } = ctx.params();
    const tempResult = await this._getService().findList({ status, uid: ctx.userId, prop, sort, pageIndex, pageSize });
    const result = ctx.helper.flat(tempResult.data);
    if (result) {
      this.success(result, tempResult.count);
    } else {
      this.error('获取论坛失败');
    }
  }

  // 获取上架的论坛
  async indexShops() {
    const { ctx } = this;
    const { pageIndex, pageSize } = ctx.params();
    const tempResult = await this._getService().findList({ status: 2, pageIndex, pageSize });
    const result = ctx.helper.flat(tempResult.data);
    if (result) {
      this.success(result, tempResult.count);
    } else {
      this.error('获取论坛失败');
    }
  }

  // 获取某用户的论坛
  async indexByUser() {
    const { ctx } = this;
    const { pageIndex, pageSize } = ctx.params();
    const { uid } = ctx.params;
    const tempResult = await this._getService().findList({ status: 2, pageIndex, pageSize, uid });
    const result = ctx.helper.flat(tempResult.data);
    if (result) {
      this.success(result, tempResult.count);
    } else {
      this.error('获取论坛失败');
    }
  }

  // 获取待审核的论坛
  async indexExamine() {
    const { ctx } = this;
    const { title, prop, sort, pageIndex, pageSize } = ctx.params();
    const { createdAt } = ctx.queries;
    const tempResult = await this._getService().findList({
      title,
      status: 1,
      prop,
      sort,
      pageIndex,
      pageSize,
      createdAt,
    });
    const result = ctx.helper.flat(tempResult.data);
    if (result) {
      this.success(result, tempResult.count);
    } else {
      this.error('获取论坛失败');
    }
  }
}

module.exports = CommentController;
